package com.school.demo.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.school.demo.model.auto.Comment;
import com.school.demo.model.auto.ReplyComment;
import com.school.demo.model.auto.ResultComment;
import com.school.demo.model.auto.User;
import com.school.demo.service.ICommentService;
import com.school.demo.service.IUserService;
import com.school.demo.util.Restful;
import com.school.demo.util.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: demo
 * @description: 评论api
 * @author: Charlotte
 * @create: 2021-03-24 22:13
 **/
@Api(tags = "评论管理API")
@RequestMapping("/api/comment")
@Controller
public class CommentApiController {

    @Autowired
    ICommentService commentService;
    @Autowired
    IUserService userService;

    //
    /**TODO 后期加分页
     * 根据文章获取评论
     */
    @ApiOperation(value = "根据文章获取评论", notes = "", tags = {})
    @RequestMapping(value = "/getByNewsId", method = RequestMethod.GET)
    public @ResponseBody
    Restful list(@RequestParam(name = "id",defaultValue = "1")Integer id) throws Exception {

        if (id<=0){
            return new Restful(Status.REQUEST_PARAMETER_ERROR);
        }

        try {
            Map<Integer, ResultComment> data = new HashMap<>();
            QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Comment::getNewsId,id).eq(Comment::getParentId,-1);
            List<Comment> list = commentService.list(queryWrapper);

            User user = new User();
            for (Comment c: list) {
                ResultComment rc = new ResultComment(c);
                user = userService.getById(c.getUserId());
                if (user!= null){
                    User user1 = new User();
                    user1.setId(user.getId());
                    user1.setName(user.getName());
                    user1.setHeadImgUrl(user.getHeadImgUrl());
                    user1.setCreated(user.getCreated());
                    user1.setMobile(user.getMobile());
                    rc.setUser(user1);
                }else {}
                data.put(rc.getId(),rc);
            }

            QueryWrapper<Comment> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().eq(Comment::getNewsId,id).gt(Comment::getId,0);

            list.clear();
            list = commentService.list(queryWrapper1);

            for (Comment c:list) {
                ReplyComment rc = new ReplyComment(c);
                user = userService.getById(c.getUserId());
                if (user!= null){
                    User user1 = new User();
                    user1.setId(user.getId());
                    user1.setName(user.getName());
                    user1.setHeadImgUrl(user.getHeadImgUrl());
                    user1.setCreated(user.getCreated());
                    user1.setMobile(user.getMobile());
                    rc.setUser(user1);
                }else {}
                rc.setId(c.getId());
                rc.setNewsId(c.getNewsId());
                rc.setContent(c.getContent());
                rc.setCreatedTime(c.getCreatedTime());
                rc.setStatus(c.getStatus());

                if (data.get(c.getParentId()).getChildren() == null){
                    Map<Integer,ReplyComment> map = new HashMap<>();
                    data.get(c.getParentId()).setChildren(map);
                }
                data.get(c.getParentId()).getChildren().put(rc.getId(),rc);
            }
            Restful restful = new Restful(Status.SUCCESS);
            restful.setData(data);
            return restful;
        }catch (Exception e){
            return new Restful(Status.WARN);
        }
    }

    //添加
    @ApiOperation(value = "添加评论")
    @PostMapping("add")
    public Restful add(@RequestBody Comment comment){
        if (comment == null){
            return new Restful(Status.REQUEST_PARAMETER_ERROR);
        }
        try {
            commentService.save(comment);
            return new Restful(Status.SUCCESS);
        }catch (Exception e){
            return new Restful(Status.WARN);
        }
    }

    //删除
    @ApiOperation(value = "删除")
    @PostMapping("delete")
    public Restful delete(@RequestParam("id")Integer id){
        if (id<0){
            return new Restful(Status.REQUEST_PARAMETER_ERROR);
        }
        try {
            Comment comment = commentService.getById(id);
            if (comment != null){
                comment.setStatus(-1);
                commentService.save(comment);
                return new Restful(Status.SUCCESS);
            }
            return new Restful(Status.WARN);
        }catch (Exception e){
            return new Restful(Status.WARN);
        }
    }

    //根据ID获取评论
    @ApiOperation(value = "添加评论")
    @PostMapping("getById")
    public Restful getById(@RequestParam("id")Integer id){
        if (id<0){
            return new Restful(Status.REQUEST_PARAMETER_ERROR);
        }
        try {
            Comment comment = commentService.getById(id);

            Restful restful = new Restful(Status.SUCCESS);
            restful.setData(comment);
            return restful;
        }catch (Exception e){
            return new Restful(Status.WARN);
        }
    }

    //TODO 根据userId获取评论




}
